package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.squareup.okhttp.Request;

import java.util.HashMap;

import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.adapter.base.CommentAdapter;
import project.dade.cn.bst.adapter.base.MyorderAdapter;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.model.GsonUtils;
import project.dade.cn.bst.model.MyordrerModel;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2016/4/21.
 */
public class CommentActivity extends ZJPBaseBarActivity implements View.OnClickListener{
    @InjectView(R.id.photo)
    ImageView imageView;
    @InjectView(R.id.title)
    TextView tv_title;
    @InjectView(R.id.sell)
    TextView sell;
    @InjectView(R.id.rating_bar)
    RatingBar  ratingBar;
    @InjectView(R.id.et_content)
    EditText etcontent;
    @InjectView(R.id.btn_comment)
    Button bncomment;
    private String orderno;
    private  String orderimage;
    private String price;
    private String title;
    private String id;
    private String edit;
    private int scoreCode;
    private KProgressHUD hud;
    private CommentAdapter commentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        orderno=getIntent().getStringExtra("orderno");
        orderimage=getIntent().getStringExtra("orderimage");
        price=getIntent().getStringExtra("price");
        title=getIntent().getStringExtra("title");
        id=getIntent().getStringExtra("id");
        edit=etcontent.getText().toString().trim();
        scoreCode = (int)  ratingBar.getRating();
        Glide.with(this).load(orderimage).placeholder(R.drawable.image_loader_small).error(R.drawable.image_loader_small).into(imageView);
        tv_title.setText(title);
         sell.setText(price);
        bncomment.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_comment:
                requestCommtent();
                break;
        }
    }

    private void requestCommtent() {
        HashMap<String, String> params = new HashMap<String, String>();
        hud = KProgressHUD.create(CommentActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在加载")
                .setCancellable(true);
        hud.show();
        params.put("uid", AppApplication.getInstance().getSpUtil().getUserID().toString());
        params.put("mobilephone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
        params.put("orderno",orderno);
        params.put("proid",id);
        params.put("content",edit);
        params.put("grade",scoreCode+"");
        params.put("Sign", FormatRequest.main(params));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Addcomment).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        hud.dismiss();
                        ToastUtil.showShort(CommentActivity.this, "网络相应失败请稍后再试");
                    }
                    @Override
                    public void onResponse(String response) {
                     hud.dismiss();
                        LogUtil.i(TAG, "获取订单列表：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取订单列表"))) {
                            LogUtil.i(TAG, "评论订单：" + response);

                            BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                            if (model != null && model.getResult() == 0) {
                                Intent mIntent = new Intent(CommentActivity.this,
                                        MyorderActivity.class);
                                startActivity(mIntent);
                                finish();
                            }

                        }else {
                          hud.dismiss();
                            ToastUtil.showShort(CommentActivity.this, NetRequestUtil.requestsError(response, ""));
                        }
                    }

                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
